const Validator = require('validator')
const isEmpty = require('./isEmpty')
module.exports = function validateRegisterIput(data) {
    console.log(4)
    let errors = {}
    for (let key in data) {
        if (isEmpty(data[key])) {
            key = ''
        }
    }

    if (Validator.isEmpty(data.name)) {
        errors.name = '名字不能为空！'
    }
    if (!Validator.isLength(data.name, { min: 2, max: 10 })) {
        errors.name = '名字长度不能小于2位且不能超过10位！'
    }
    if (Validator.isEmpty(data.email)) {
        errors.email = '邮箱不能为空！'
    }
    if (!Validator.isEmail(data.email)) {
        errors.email = '邮箱不合法！'
    }
    if (Validator.isEmpty(data.password)) {
        errors.password = '密码不能为空！'
    }
    if (!Validator.isLength(data.password, { min: 2, max: 10 })) {
        errors.password = '密码长度不能小于2位且不能超过10位！'
    }
    if (Validator.isEmpty(data.password2)) {
        errors.password2 = 'password2不能为空！'
    }
    if (!(data.password === data.password2)) {
        errors.password2 = '两次输入的密码不一致！'
    }
    if (data.admin) {
        if (typeof (data.admin) === Boolean && data.admin === false) {
            errors.admin = '普通账户！'
        }
    }
    return { errors, isValid: isEmpty(errors) }
}
